<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">


      <el-form-item label="日期类型" prop="afterType">
        <el-select v-model="tjDateType" placeholder="请选择日期类型" @change="handleTjDateType">
          <el-option
            v-for="dict in dict.type.tj_date_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item v-if="tjDateType === 'y'" label="年份" prop="afterDate">
        <el-date-picker
          v-model="queryParams.statisticsYear"
          type="year"
          placeholder="选择年份"
          value-format="yyyy"
          :size="size"
        />
      </el-form-item>

      <el-form-item v-if="tjDateType === 'm'" label="月份" prop="afterDate">
        <el-date-picker
          v-model="queryParams.statisticsMonth"
          type="month"
          placeholder="选择月份"
          value-format="yyyy-MM"
          :size="size"
        />
      </el-form-item>

      <el-form-item v-if="tjDateType === 'd'" label="日期" prop="afterDate">
        <el-date-picker
          v-model="queryParams.statisticsDay"
          type="date"
          placeholder="选择日期"
          value-format="yyyy-MM-dd"
          :size="size"
        />
      </el-form-item>

      <el-form-item label="收支类型" prop="afterType">
        <el-select v-model="queryParams.fundingType" placeholder="请选择收支类型" clearable>
          <el-option
            v-for="dict in dict.type.funding_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <left-toolbar>
        <el-button size="mini" @click="tjQuery(1)">今年</el-button>
        <el-button size="mini" @click="tjQuery(2)">本月</el-button>
      </left-toolbar>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="tjList" border style="width: 100%">
      <el-table-column type="selection" width="55" align="center" />

      <el-table-column label="总计(元)" align="center" prop="fundingAmount" />
      <el-table-column label="类型" align="center" prop="fundingType">
        <template slot-scope="scope">
          {{scope.row.fundingType == 1 ? '收入' : '支出'}}
        </template>
      </el-table-column>
      <el-table-column label="年" align="center" prop="statisticsYear" />
      <el-table-column label="月" align="center" prop="statisticsMonth" />
      <el-table-column label="日" align="center" prop="statisticsDay" />

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleDetailQuery(scope.row)"
          >查看明细</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />



    <!--完成-->
    <el-dialog title="明细" :visible.sync="open" width="900px" append-to-body>
      <el-table v-loading="detailsLoading" :data="detailsList" border style="width: 100%">
        <el-table-column type="selection" width="55" align="center" />

        <el-table-column label="金额" align="center" prop="fundingAmount" />
        <el-table-column label="类型" align="center" prop="fundingType">
          <template slot-scope="scope">
            {{scope.row.fundingType == 1 ? '收入' : '支出'}}
          </template>
        </el-table-column>
        <el-table-column label="日期" align="center" prop="fundingDate" >
          <template slot-scope="scope">
            {{dateFormat(scope.row)}}
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center" prop="fundingRemark" />

<!--        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              @click="detailList(scope.row)"
            >查看明细</el-button>
          </template>
        </el-table-column>-->
      </el-table>

      <pagination
        v-show="detailsTotal>0"
        :total="detailsTotal"
        :page.sync="detailsListQueryParams.pageNum"
        :limit.sync="detailsListQueryParams.pageSize"
        @pagination="getMXList"
      />
    </el-dialog>

  </div>
</template>

<script>
import {getDetailList, listTJ} from "@/api/system/artFundingDetails";

export default {
  name: "OrderAfter",
  dicts: ['funding_type', 'tj_date_type'],
  data() {
    return {
      tjDateType: null,
      // 遮罩层
      loading: true,
      detailsLoading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      detailsTotal: 0,
      // 售后订单表格数据
      tjList: [],
      detailsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fundingType: '1',
        groupBy: null,
        statisticsYear: null,
        statisticsMonth: null,
        statisticsDay: null
      },
      detailsListQueryParams: {
        pageNum: 1,
        pageSize: 10
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    // 默认查询当天的收入统计
    this.getList();
  },
  methods: {
    /** 查询售后订单列表 */
    getList() {
      this.loading = true;
      listTJ(this.queryParams).then(response => {
        this.tjList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getMXList(row) {
      this.detailsLoading = true;

      this.detailsListQueryParams = {fundingType: row.fundingType};

      if(row.statisticsYear){
        this.detailsListQueryParams.statisticsYear = row.statisticsYear;
      }else if(row.statisticsMonth){
        this.detailsListQueryParams.statisticsMonth = row.statisticsMonth;
      }else if(row.statisticsDay){
        this.detailsListQueryParams.statisticsDay = row.statisticsDay;
      }

      getDetailList(this.detailsListQueryParams).then(response => {
        this.detailsList = response.rows;
        this.detailsTotal = response.total;
        this.detailsLoading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    tjQuery(type) {
      this.resetQueryParams();
      let now = new Date();
      let year = now.getFullYear();
      let month = now.getMonth() + 1;
      let day = now.getDate();

      if(month < 10){
        month = '0' + month;
      }

      if(day < 10){
        day = '0' + day;
      }

      if(type == 1){
        this.queryParams.groupBy = 'month';
        this.queryParams.statisticsYear = year + '';
      }else if(type == 2){
        this.queryParams.groupBy = 'day';
        this.queryParams.statisticsMonth = year + '-' + month;
      }

      this.getList();
    },
    dateFormat(row) {
      // 假设日期格式为 'YYYY-MM-DD'，我们将其格式化为 'YYYY年MM月DD日'
      const date = new Date(row.fundingDate);
      const year = date.getFullYear();
      const month = (date.getMonth() + 1).toString().padStart(2, '0');
      const day = date.getDate().toString().padStart(2, '0');
      const hours = date.getHours().toString().padStart(2, '0');
      const minutes = date.getMinutes().toString().padStart(2, '0');
      const seconds = date.getSeconds().toString().padStart(2, '0');
      return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
    },
    resetQueryParams() {
      this.queryParams.pageNum = 1;
      this.queryParams.statisticsYear = null;
      this.queryParams.statisticsMonth = null;
      this.queryParams.statisticsDay = null;
    },
    handleTjDateType(value){
      this.queryParams.statisticsYear = null;
      this.queryParams.statisticsMonth = null;
      this.queryParams.statisticsDay = null;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;

      if(this.tjDateType === 'y'){
        this.queryParams.groupBy = 'month';
      }else if(this.tjDateType === 'm'){
        this.queryParams.groupBy = 'day';
      }

      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetQueryParams();
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    handleDetailQuery(row) {
      this.detailsListQueryParams.pageNum = 1;
      this.getMXList(row);
      this.open = true;
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/orderAfter/export', {
        ...this.queryParams
      }, `orderAfter_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
